package cn.gov.msa.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import sun.misc.BASE64Decoder;

import cn.org.bjca.seal.esspdf.client.message.ChannelMessage;
import cn.org.bjca.seal.esspdf.client.message.ClientSignBean;
import cn.org.bjca.seal.esspdf.client.message.ClientSignMessage;
import cn.org.bjca.seal.esspdf.client.tools.ESSPDFClientTool;
import cn.org.bjca.seal.esspdf.client.utils.ClientUtil;


public class ESSPDFClientToolUtil {
	
	public static Boolean pdfSign(String ruleNum,String oldPath,String newPath) throws Exception {//PDF签章测试
		try {
			System.out.println("进入pdf签章");
			System.out.println(PropertiesTools.readProperties("ess.ip"));
			System.out.println(PropertiesTools.readProperties("ess.port"));
			ESSPDFClientTool essPDFClientTool = new ESSPDFClientTool(PropertiesTools.readProperties("ess.ip"), Integer.parseInt(PropertiesTools.readProperties("ess.port")));
			//服务器直连
			essPDFClientTool.setTimeout(5000);
			//服务器响应时间
			essPDFClientTool.setRespTimeout(5000);
			byte[] pdfBty = ClientUtil.readFileToByteArray(new File(oldPath));//pdf字节数组
			long begin = System.currentTimeMillis();
			ChannelMessage message = essPDFClientTool.pdfSign(ruleNum, pdfBty);
			long end = System.currentTimeMillis();
			System.out.println("运行时间:"+(end - begin)/1000f+"s");
			System.out.println("状态码："+message.getStatusCode());
			System.out.println("状态信息："+message.getStatusInfo());
			if("200".equals(message.getStatusCode())){//成功
				ClientUtil.writeByteArrayToFile(new File(newPath), message.getBody());
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			System.out.println("签章失败");
			e.printStackTrace();
			return false;
		}
	}
}
